/** @file triangle.hpp Triangle intersection function */
#pragma once

class Triangle;
#include "core/geometry.hpp"
#include "core/isect.hpp"
#include "core/mesh.hpp"

/** A single triangle */
struct Triangle
{
  Mesh* parent;
  Normal n;
  int index;
  Triangle(Mesh* p, int i) : parent(p), index(i)
  {
    Point v0,v1,v2;
    getData(&v0,&v1,&v2);
    n = cross(Normal(v1-v0),Normal(v2-v0));
  }
  Triangle() {}
  void getData(Point* v0, Point* v1, Point* v2) const;
};

/** Intersect a ray with a triangle
 * If isect is NULL, then it will not bother to compute details
 * @param t triangle to intersect
 * @param r ray in object space
 * @param isect intersection pointer
 * @param maxt maximum intersection time
 * @return if there was a hit */
extern bool isectTriangle(const Triangle& t, const Ray& r, Intersection* isect, const float maxt=INFINITY);
